package com.matrix.math;

import junit.framework.Assert;
import junit.framework.TestCase;

/**
 * @author Sultan-PC
 *
 */

public class TransposeTest extends TestCase
{
	// variables
	
	Matrix m;
	double delta = 1e-15;
	double[][] testArray = new double[][]{
			{1.5, 2.5, 3.5}, {1.5, 2.5, 3.5}
			};
	double[][] expectedResult = new double[][]{
			{1.5, 1.5}, {2.5, 2.5}, {3.5,3.5}
	};
	
	/**
	 * Test method for {@link Matrix#transpose(double[][])}.
	 */
	public void testTranspose()
	{
		m = new Matrix("A",testArray);
		Matrix actuals = null;
		try {
			actuals = m.transpose();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if(!m.equals(actuals))
			{
				fail();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * The JUnit framework doesn't provide a method for testing
	 * equality of two 2d arrays, so I added this one.
	 */
	public static void assertArrayEquals(double[][] expected, double[][] actual, double delta)
	{
		assertArrayEquals("", expected, actual, delta);
	}
	
	/**
	 * Parent Method for the previous one.
	 * @param str
	 * @param expected
	 * @param actuals
	 * @param delta
	 */
	public static void assertArrayEquals(String str,double[][] expected, double[][] actuals, double delta)
	{
		String header = str == null ? "" : str + ": ";
		assertArrayAreSameLength(expected,actuals, header);
		
		for(int i = 0; i < expected.length; ++i)
			for(int j = 0; j < expected[0].length; ++j)
			{
				assertEquals(expected[i][j], actuals[i][j], 1e-15);
			}
	}
	/**
	 * method to check if two 2d arrays have the same lengths
	 * @param expected
	 * @param actuals
	 * @param header
	 */
	public static void assertArrayAreSameLength(double[][] expected, double[][] actuals, String header)
	{
		if(expected == null) Assert.fail(header+" expected array was null");
		if(actuals == null) Assert.fail(header + " actuals array was null");
		int expectedLengthRows = expected.length;
		int expectedLengthCols = expected[0].length;
		int actualsLengthRows = expected.length;
		int actualsLengthCols = expected[0].length;
		if(expectedLengthRows != actualsLengthRows && expectedLengthCols != actualsLengthCols)
		{
			Assert.fail(header + "Array Lengths differed, expected.row = "+expectedLengthRows+"  expected.cols = "+expectedLengthCols+" and" +
					"actuals.rows = "+actualsLengthRows+"  actuals.cols = "+actualsLengthCols);
		}
	}

}
